// Copyright (C) 2015, Alberto Corona <alberto@0x1a.us>
// All rights reserved. This file is part of core-utils, distributed under the
// GPL v3 license. For full terms please see the LICENSE file.

#![crate_type = "lib"]
#![feature(path_relative_from)]

extern crate term;
use std::io::prelude::Write;

use std::process;
use std::path::{PathBuf};

pub enum Status {
    Ok,
    Error,
    OptError,
}

pub fn exit(status: Status) {
    process::exit(status as i32);
}

pub fn to_rel(path: &PathBuf, rel_from: &PathBuf) -> PathBuf {
    match path.relative_from(&rel_from) {
        Some(s) => { return s.to_path_buf() },
        None => { panic!() },
    };
}

pub fn path_err(status: Status, mesg: String, item: PathBuf) {
    match term::stdout() {
        Some(mut term) => {
            term.fg(term::color::RED).unwrap();
            (write!(term, "{}: {}\n",item.display(), mesg)).unwrap();
            exit(status);
        }
        None => {},
    };
}

pub fn err(prog: &str, status: Status, mesg: String) {
    match term::stdout() {
        Some(mut term) => {
            term.fg(term::color::RED).unwrap();
            (write!(term, "{}: {}\n", prog, mesg)).unwrap();
            exit(status);
        }
        None => {},
    };
}

pub fn join_vects(first: Vec<String>, second: Vec<String>) -> Vec<String> {
    let mut collection: Vec<String> = first;
    for i in second.iter() {
        collection.push(i.clone());
    }
    return collection;
}

pub fn copyright(prog: &str, vers: &str, yr: &str, auth: Vec<&str>) {
    print!("{} (core-utils) {}\n\
    Copyright (C) {} core-utils developers\n\
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\
    This is free software: you are free to change and redistribute it.\n\
    There is NO WARRANTY, to the extent permitted by law.\n\n", prog, vers, yr);
    print!("Written by ");
    for pers in auth.iter() {
        print!("{} ", pers);
    }
    print!("\n");
}
